package com.draw.cli.gui.panel.login;

import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.xml.ws.WebServiceException;

import com.draw.cli.config.ConfigManager;
import com.draw.cli.gui.Loading;
import com.draw.cli.gui.StateBar;
import com.draw.cli.gui.Window;
import com.draw.cli.gui.panel.Refreshable;
import com.draw.cli.gui.panel.home.HomePanel;
import com.draw.cli.webclient.UserClient;
import com.draw.core.User;
import com.draw.webinterface.UserInterface;

public class LoginPanel extends JPanel implements ActionListener, Runnable,
		Refreshable {
	private static final long serialVersionUID = -501761634761648404L;

	public static final String LOGIN_FAILED = "L'utilisateur n'existe pas.";
	public static final String LOGIN_SUCCESS = "Vous êtes maintenant connecté.";

	private static LoginPanel instance = new LoginPanel();

	public static LoginPanel getInstance() {
		return instance;
	}

	private JTextField usernameField;
	private JTextField passwordField;
	private JButton ok;
	private JButton cancel;

	private Thread thread;

	public LoginPanel() {
		this.thread = null;

		this.setLayout(new FlowLayout());

		JPanel pan = new JPanel(new GridLayout(3, 2));

		pan.setBorder(BorderFactory.createTitledBorder("Identification"));

		pan.add(new JLabel("Nom d'utilisateur : "));
		this.usernameField = new JTextField();
		this.usernameField.setPreferredSize(new Dimension(100, 10));
		pan.add(this.usernameField);

		pan.add(new JLabel("Mot de passe : "));
		this.passwordField = new JPasswordField();
		this.passwordField.setPreferredSize(new Dimension(100, 10));
		pan.add(this.passwordField);

		this.ok = new JButton("OK");
		this.ok.addActionListener(this);
		pan.add(this.ok);

		this.cancel = new JButton("Annuler");
		this.cancel.addActionListener(this);
		pan.add(this.cancel);
		
		this.refresh();

		this.add(pan);
	}

	public void actionPerformed(ActionEvent arg0) {
		if (this.ok.equals(arg0.getSource())) {
			this.thread = new Thread(this);
			this.thread.start();
		} else {
			Window.getInstance().setHome();
		}
	}

	private void login() {
		this.ok.setEnabled(false);
		this.cancel.setEnabled(false);

		try {
			UserInterface ui = UserClient.getInstance();
			Loading.start();
			User user = ui.getUser(this.usernameField.getText(),
					this.passwordField.getText());
			Loading.stop();
			if (user == null) {
				JOptionPane.showMessageDialog(this, LOGIN_FAILED,
						"Identification", JOptionPane.ERROR_MESSAGE);
			} else {
				JOptionPane.showMessageDialog(this, LOGIN_SUCCESS,
						"Identification", JOptionPane.INFORMATION_MESSAGE);
				Loading.start();
				ConfigManager.getInstance().setCurrentUser(user);
				HomePanel.getInstance().refresh();
				Window.getInstance().setHome();
				Loading.stop();
			}
		} catch (MalformedURLException | WebServiceException e) {
			StateBar.getInstance().setServerOffline();
			JOptionPane.showMessageDialog(this, StateBar.SERVER_OFFLINE,
					"Identification", JOptionPane.ERROR_MESSAGE);
		}

		this.ok.setEnabled(true);
		this.cancel.setEnabled(true);
		this.thread = null;
	}

	@Override
	public void run() {
		this.login();
	}

	@Override
	public void refresh() {
		this.usernameField.setText(ConfigManager.getInstance().getUsername());
		this.passwordField.setText(ConfigManager.getInstance().getPassword());
	}
}
